import backend.app.app.models.db_sys_models as md
from backend.app.app.db.session_sys import session
from backend.app.app.models.user import user_info_revise

#获取所有信息
def get_old_(uid:id):
    try:
        tb_user_inf = session.query(md.TbUser.User_ID,
                                    md.TbUser.User_Name,
                                    md.TbUser.User_PWD,
                                    md.TbUser.Belong_Depart,
                                    md.TbUser.Job_Name,
                                    md.TbUser.User_TruName,
                                    md.TbUser.User_MobileNumber,
                                    md.TbUser.User_Email,
                                    md.TbUser.Register_Date,
                                    md.TbUser.IsQualified,
                                    md.TbUser.Remark).filter(md.TbUser.User_ID == uid.user_id).all()

        return tb_user_inf
    except:
        print('查找信息失败')
        session.rollback()
        return False

def revise_user(user:user_info_revise):
    try:
        session.query(md.TbUser).filter(md.TbUser.User_ID == user.User_ID).update(
            {md.TbUser.User_Name:user.User_Name,
             md.TbUser.User_PWD:user.User_PWD,
             md.TbUser.User_MobileNumber:user.User_MobileNumber,
             md.TbUser.User_Email:user.User_Email})
        session.commit()
        print('更新用户信息成功')
        return True
    except:
        print('更新用户信息失败')
        session.rollback()
        return False


#获取所有管理人员需要处理信息
def get_all_user_inf():

    try:
        tb_user_inf = session.query(md.TbUser.User_ID,
                                    md.TbUser.User_Name,
                                    md.TbUser.User_PWD,
                                    md.TbUser.Belong_Depart,
                                    md.TbUser.Job_Name,
                                    md.TbUser.User_TruName,
                                    md.TbUser.User_MobileNumber,
                                    md.TbUser.User_Email,
                                    md.TbUser.Register_Date,
                                    md.TbUser.IsQualified,
                                    md.TbUser.Remark).all()

        return tb_user_inf
    except:
        print('查找tb_user_inf失败')
        session.rollback()
        return False

def get_online_tb(user_id):
    try:
        legel_id = session.query(md.TbUser.LegalPerson_ID).filter(md.TbUser.User_ID == user_id).first()
        tb_user_online = session.query(md.TbUser.Online).filter(md.TbUser.LegalPerson_ID == legel_id[0]
                                                                ,md.TbUser.Online == 1).all()
        return tb_user_online
    except:
        print('查询在线人数失败')
        session.rollback()
        return False

def get_all_online():
    try:
        tb_user_online = session.query(md.TbUser.Online).filter(md.TbUser.Online == 1).all()
        return tb_user_online
    except:
        print('查询在线人数失败')
        session.rollback()
        return False

def updata_online_tb(user_id,status):
    try:
        session.query(md.TbUser).filter(md.TbUser.User_ID == user_id).update(
            {md.TbUser.Online:status})
        session.commit()
        print('更新用户在线状态成功')
        return True
    except:
        print('更新用户在线状态失败')
        session.rollback()
        return False